﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Core;

namespace GraphControls
{
    public partial class PortProperty : Form
    {
        private ProjectsControl _projectsControl;
        private int _widh=0;
        private int _hight=0;
        private Node _node;

        public PortProperty(ProjectsControl projectsControl, Node node,int widh, int hight)
        {
            _node = node;
            _widh = widh;
            _hight = hight;
            _projectsControl = projectsControl;
            InitializeComponent();
            var items = _projectsControl.GetProject().Schems.Where(x => x.TypeOf == Graph.TypeOfGraph.Chenal);
            foreach (Graph graph in items)
            {
                comboBox1.Items.Add(graph.Name);
            }
            items = _projectsControl.GetProject().Schems.Where(x => x.TypeOf == Graph.TypeOfGraph.Process);
            foreach (Graph graph in items)
            {
                comboBox2.Items.Add(graph.Name);
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var items = _projectsControl.GetProject().Schems.Where(x => x.Name == comboBox2.SelectedItem).ToArray();
            if((comboBox1.SelectedIndex >= 0) && (comboBox2.SelectedIndex >= 0))
            {
                TypeOfNode typeOfNode;
                if (_node.Nodetype == TypeOfNode.Client)
                    typeOfNode = TypeOfNode.Server;
                else
                {
                    typeOfNode = TypeOfNode.Client;
                }
                Node t =items[0].AddNode(_widh / 2,_hight /2,_node.Id,(byte) typeOfNode);
                t.SetChanel(comboBox1.SelectedItem.ToString());
                _node.SetChanel(comboBox1.SelectedItem.ToString());
                this.Close();
            }
            else
            {
                MessageBox.Show(this, "Обязательно нужно выбрать процесс и канал",
                        "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
